package com.SDU.policedispatch.service.impl.privilege;

import com.SDU.policedispatch.entity.Department;
import com.SDU.policedispatch.entity.User;
import com.SDU.policedispatch.mapper.DepartmentMapper;
import com.SDU.policedispatch.service.privilege.GetPrivilegeService;
import com.SDU.policedispatch.service.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetPrivilegeServiceImpl implements GetPrivilegeService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Map<String, Object> GetP() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Map<String, Object> map = new HashMap<>();
        boolean Super = user.getAccountType() != 1;

        List<Department> deptList = departmentMapper.selectList(null);
        List<String> list = new ArrayList<>();
        for(Department d: deptList) {
            if(Objects.equals(d.getDeptId(), user.getDeptId().toString()) && !Super) {
                list.add(d.getDeptName());
                break;
            }
            if(Super) {
                list.add(d.getDeptName());
            }
        }
        map.put("super", Super);
        map.put("departments", list);
        return map;
    }
}
